# -*- coding: utf-8 -*-
# @Time    : 2020/3/12 下午6:18
# @Author  : lilong


class Singleton1(object):
    """单例实现方式二"""

    # 类属性
    __instance = None
    __isFirstInit = False

    def __new__(cls, name):
        if not cls.__instance:
            Singleton1.__instance = super().__new__(cls)
        return cls.__instance   # 一定要返回该类，因为要被__init__初始化

    def __init__(self, name):
        if not self.__isFirstInit:
            self.__name = name
            Singleton1.__isFirstInit = True

    def getName(self):
        return self.__name


# 测试
tony = Singleton1("Tony")
karry = Singleton1("Karry")
print(tony.getName(), karry.getName())
print("id(tony):", id(tony), "id(karry):", id(karry))
print("tony == karry:", tony == karry)
